package com.halloon.android.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.halloon.android.R;
import com.halloon.android.task.PostActionTask;
import com.halloon.android.util.Constants;

import com.actionbarsherlock.app.SherlockFragment;

public class CommentFragment extends SherlockFragment implements OnClickListener{

	private CommentFragmentCallback cfCallback;
	private Context context;
	
	private TextView title;
	private EditText content;
	private ImageView emojiButton;
	private ImageView atButton;
	private ImageView hotButton;
	private Button publicButton;
	private Button backButton;
	
	private String tweetId;
	
	public static final int COMMENT = 0;
	public static final int RETWEET = 1;
	
	private int type = COMMENT;
	
	public interface CommentFragmentCallback{
		public void destroyCommentFragment();
	}

	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
		this.context = activity;
		this.cfCallback = (CommentFragmentCallback) activity;
		this.tweetId = getArguments().getString("id");
		this.type = getArguments().getInt("type");
	}
	
	@Override
	public void onStart(){
		super.onStart();
		Intent intent = new Intent();
		intent.setAction(Constants.GLOBAL_TAB_VISIBILITY);
		Bundle bundle = new Bundle();
		bundle.putBoolean("isTabShow", false);
		intent.putExtras(bundle);
		context.sendBroadcast(intent);
	}
	
	@Override
	public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState){
		View root = layoutInflater.inflate(R.layout.edit_comment_content, null, false);
		
		title = (TextView) root.findViewById(R.id.title_text);
		content = (EditText) root.findViewById(R.id.edit_comment);
		emojiButton = (ImageView) root.findViewById(R.id.p_emoji);
		atButton = (ImageView) root.findViewById(R.id.p_at);
		hotButton = (ImageView) root.findViewById(R.id.p_sharp);
		publicButton = (Button) root.findViewById(R.id.publish);
		backButton = (Button) root.findViewById(R.id.back_button);
		
		emojiButton.setOnClickListener(this);
		atButton.setOnClickListener(this);
		hotButton.setOnClickListener(this);
		publicButton.setOnClickListener(this);
		backButton.setOnClickListener(this);
		
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		if(type == COMMENT){
			title.setText(context.getString(R.string.comment));
		}else{
			title.setText(context.getString(R.string.retweet));
		}
	}
	
	@Override
	public void onStop(){
		super.onStop();
		Intent intent = new Intent();
		intent.setAction(Constants.GLOBAL_TAB_VISIBILITY);
		Bundle bundle = new Bundle();
		bundle.putBoolean("isTabShow", true);
		intent.putExtras(bundle);
		context.sendBroadcast(intent);
	}
	
	@Override
	public void onClick(View v){
		switch(v.getId()){
		case R.id.p_emoji:
			break;
		case R.id.p_at:
			break;
		case R.id.p_sharp:
			break;
		case R.id.publish:
			PostActionTask task;
			if(type == COMMENT){
				task = new PostActionTask(context, PostActionTask.COMMENT, tweetId, content.getText().toString());
			}else{
				task = new PostActionTask(context, PostActionTask.RETWEET, tweetId, content.getText().toString());
			}
			
			if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
				task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
			}else{
				task.execute();
			}
			break;
		case R.id.back_button:
			cfCallback.destroyCommentFragment();
			break;
		}
	}
}
